#include "ZMouseArea.h"

namespace ZuiLib
{

ZMouseArea::ZMouseArea()
	:m_uButtonState(0)
{
	m_bMouseEnabled = true;
	m_bExpandChild = true;
}

bool ZMouseArea::Activate()
{
	if (!ZControl::Activate()) return false;
	SignalClick(this);
	return true;
}

void ZMouseArea::DoEvent(TEventUI& event)
{
	if (IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND) {
		if (m_pParent != NULL) 
			m_pParent->DoEvent(event);
		else 
			ZContainer::DoEvent(event);
	}

	switch (event.Type)
	{
		case UIEVENT_BUTTONDOWN:
		{
			if (PtInRect(&m_rcItem, event.ptMouse) && IsEnabled()) {
				m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
				SignalMouseDown(this);
			}
			break;
		}
		case UIEVENT_RBUTTONDOWN:
		{
			if (PtInRect(&m_rcItem, event.ptMouse) && IsEnabled()) {
				m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
			}
			break;
		}
		case UIEVENT_DBLCLICK:
		{
			if (PtInRect(&m_rcItem, event.ptMouse) && IsEnabled()) {
				m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
				SignalDbClick(this);
			}
			break;
		}
		case UIEVENT_MOUSEMOVE:
		{
			if ((m_uButtonState & UISTATE_CAPTURED) != 0) {
				if (PtInRect(&m_rcItem, event.ptMouse))
					m_uButtonState |= UISTATE_PUSHED;
				else
					m_uButtonState &= ~UISTATE_PUSHED;

				SignalMouseMove(this, event.ptMouse);
			}
			break;
		}
		case UIEVENT_BUTTONUP:
		{
			if ((m_uButtonState & UISTATE_CAPTURED) != 0) {
				if (PtInRect(&m_rcItem, event.ptMouse))
					Activate();
				m_uButtonState &= ~(UISTATE_PUSHED | UISTATE_CAPTURED);
				SignalMouseUp(this);
			}
			break;
		}
		case UIEVENT_RBUTTONUP:
		{
			if ((m_uButtonState & UISTATE_CAPTURED) != 0) {
				if (PtInRect(&m_rcItem, event.ptMouse))
				{
					SignalRClick(this);
				}
				m_uButtonState &= ~(UISTATE_PUSHED | UISTATE_CAPTURED);
			}
			break;
		}
		case UIEVENT_MOUSEENTER:
		{
			if (IsEnabled()) {
				m_uButtonState |= UISTATE_HOT;
				SignalMouseEnter(this);
			}
			break;
		}
		case UIEVENT_MOUSELEAVE:
		{
			if (IsEnabled()) {
				m_uButtonState &= ~UISTATE_HOT;
				SignalMouseLeave(this);
			}
			break;
		}
	}

}

void ZMouseArea::SetAttribute(const char* pstrName, const char* pstrValue)
{
	if (stricmp(pstrName, "onclick") == 0)
	{
		SetEventAttr("onClick", pstrValue);
		SignalClick.connect((ZLuaWindow*)GetWindow(), &ZLuaWindow::onMouseAreaClick);
	}
	else if (stricmp(pstrName, "onrclick") == 0)
	{
		SetEventAttr("onRClick", pstrValue);
		SignalRClick.connect((ZLuaWindow*)GetWindow(), &ZLuaWindow::onMouseAreaRClick);
	}
	else if (stricmp(pstrName, "ondbclick") == 0)
	{
		SetEventAttr("onDbClick", pstrValue);
		SignalDbClick.connect((ZLuaWindow*)GetWindow(), &ZLuaWindow::onMouseAreaDbClick);
	}
	else if (stricmp(pstrName, "onmousedown") == 0)
	{
		SetEventAttr("onMouseDown", pstrValue);
		SignalMouseDown.connect((ZLuaWindow*)GetWindow(), &ZLuaWindow::onMouseAreaMouseDown);
	}
	else if (stricmp(pstrName, "onmouseup") == 0)
	{
		SetEventAttr("onMouseUp", pstrValue);
		SignalMouseUp.connect((ZLuaWindow*)GetWindow(), &ZLuaWindow::onMouseAreaMouseUp);
	}
	else if (stricmp(pstrName, "onmouseenter") == 0)
	{
		SetEventAttr("onMouseEnter", pstrValue);
		SignalMouseEnter.connect((ZLuaWindow*)GetWindow(), &ZLuaWindow::onMouseAreaMouseEnter);
	}
	else if (stricmp(pstrName, "onmouseleave") == 0)
	{
		SetEventAttr("onMouseLeave", pstrValue);
		SignalMouseLeave.connect((ZLuaWindow*)GetWindow(), &ZLuaWindow::onMouseAreaMouseLeave);
	}
	else if (stricmp(pstrName, "onmousemove") == 0)
	{
		SetEventAttr("onMouseMove", pstrValue);
		SignalMouseMove.connect((ZLuaWindow*)GetWindow(), &ZLuaWindow::onMouseAreaMouseMove);
	}
}


}//namespace
